home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 7 / Example 7.1 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  5.6 KB  |  224 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 7.1: Loading the Bone Hierarchy                    //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "skinnedMesh.h"
  10.  
  11. class APPLICATION
  12. {
  13.     public:
  14.         APPLICATION();
  15.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  16.         HRESULT Update(float deltaTime);
  17.         HRESULT Render();
  18.         HRESULT Cleanup();
  19.         HRESULT Quit();
  20.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  21.  
  22.     private:
  23.         IDirect3DDevice9* m_pDevice; 
  24.         SKINNEDMESH m_skinnedMesh;
  25.  
  26.         float m_angle;
  27.         HWND m_mainWindow;
  28. };
  29.  
  30. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  31. {
  32.     APPLICATION app;
  33.  
  34.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  35.         return 0;
  36.  
  37.     MSG msg;
  38.     memset(&msg, 0, sizeof(MSG));
  39.     int startTime = timeGetTime(); 
  40.  
  41.     while(msg.message != WM_QUIT)
  42.     {
  43.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  44.         {
  45.             ::TranslateMessage(&msg);
  46.             ::DispatchMessage(&msg);
  47.         }
  48.         else
  49.         {    
  50.             int t = timeGetTime();
  51.             float deltaTime = (t - startTime)*0.001f;
  52.  
  53.             app.Update(deltaTime);
  54.             app.Render();
  55.  
  56.             startTime = t;
  57.         }
  58.     }
  59.  
  60.     app.Cleanup();
  61.  
  62.     return msg.wParam;
  63. }
  64.  
  65. APPLICATION::APPLICATION()
  66. {
  67.     m_pDevice = NULL; 
  68.     m_mainWindow = 0;
  69.     srand(GetTickCount());
  70.     m_angle = 0.0f;
  71. }
  72.  
  73. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  74. {
  75.     debug.Print("Application initiated");
  76.  
  77.     //Create Window Class
  78.     WNDCLASS wc;
  79.     memset(&wc, 0, sizeof(WNDCLASS));
  80.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  81.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  82.     wc.hInstance     = hInstance;
  83.     wc.lpszClassName = "D3DWND";
  84.  
  85.     //Register Class and Create new Window
  86.     RegisterClass(&wc);
  87.     m_mainWindow = CreateWindow("D3DWND", "Example 7.1: Loading the Bone Hierarchy", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  88.     SetCursor(NULL);
  89.     ShowWindow(m_mainWindow, SW_SHOW);
  90.     UpdateWindow(m_mainWindow);
  91.  
  92.     //Create IDirect3D9 Interface
  93.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  94.  
  95.     if(d3d9 == NULL)
  96.     {
  97.         debug.Print("Direct3DCreate9() - FAILED");
  98.         return E_FAIL;
  99.     }
  100.  
  101.     //Check that the Device supports what we need from it
  102.     D3DCAPS9 caps;
  103.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  104.  
  105.     //Hardware Vertex Processing or not?
  106.     int vp = 0;
  107.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  108.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  109.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  110.  
  111.     //Check vertex & pixelshader versions
  112.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  113.     {
  114.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  115.     }
  116.  
  117.     //Set D3DPRESENT_PARAMETERS
  118.     D3DPRESENT_PARAMETERS d3dpp;
  119.     d3dpp.BackBufferWidth            = width;
  120.     d3dpp.BackBufferHeight           = height;
  121.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  122.     d3dpp.BackBufferCount            = 1;
  123.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  124.     d3dpp.MultiSampleQuality         = 0;
  125.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  126.     d3dpp.hDeviceWindow              = m_mainWindow;
  127.     d3dpp.Windowed                   = windowed;
  128.     d3dpp.EnableAutoDepthStencil     = true; 
  129.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  130.     d3dpp.Flags                      = 0;
  131.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  132.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  133.  
  134.     //Create the IDirect3DDevice9
  135.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  136.                                  vp, &d3dpp, &m_pDevice)))
  137.     {
  138.         debug.Print("Failed to create IDirect3DDevice9");
  139.         return E_FAIL;
  140.     }
  141.  
  142.     //Release IDirect3D9 interface
  143.     d3d9->Release();
  144.  
  145.     m_skinnedMesh.Load("mesh/drone.x", m_pDevice);
  146.  
  147.     //Set sampler state
  148.     for(int i=0;i<4;i++)
  149.     {
  150.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  151.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  152.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  153.     }
  154.  
  155.     return S_OK;
  156. }
  157.  
  158. HRESULT APPLICATION::Update(float deltaTime)
  159. {    
  160.     m_angle += deltaTime * 0.5f;
  161.     if(m_angle > D3DX_PI * 2.0f)
  162.         m_angle -= D3DX_PI * 2.0f;
  163.         
  164.     if(KEYDOWN(VK_ESCAPE))
  165.     {
  166.         Quit();
  167.     }
  168.  
  169.     return S_OK;
  170. }    
  171.  
  172. HRESULT APPLICATION::Render()
  173. {
  174.     // Clear the viewport
  175.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  176.  
  177.     //Set camera
  178.     D3DXMATRIX view, proj, world;
  179.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 10.0f, -50.0f), &D3DXVECTOR3(0.0f, 3.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  180.     D3DXMatrixOrthoLH(&proj, 10.0f, 9.0f, 0.1f, 1000.0f);
  181.     
  182.     //Set Skeleton to rotate around the Y-axis
  183.     D3DXMATRIX r, s;
  184.     D3DXMatrixRotationYawPitchRoll(&r, m_angle, 0.0f, 0.0f);
  185.     D3DXMatrixScaling(&s, 1.3f, 1.3f, 1.3f);
  186.     world = s * r;
  187.     
  188.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  189.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  190.     m_pDevice->SetTransform(D3DTS_WORLD, &world);
  191.  
  192.     // Begin the scene 
  193.     if(SUCCEEDED(m_pDevice->BeginScene()))
  194.     {
  195.         //Render Skeleton
  196.         m_skinnedMesh.RenderSkeleton(NULL, NULL, world);
  197.  
  198.         // End the scene.
  199.         m_pDevice->EndScene();
  200.         m_pDevice->Present(0, 0, 0, 0);
  201.     }
  202.  
  203.     return S_OK;
  204. }
  205.  
  206. HRESULT APPLICATION::Cleanup()
  207. {
  208.     try
  209.     {
  210.         m_pDevice->Release();
  211.  
  212.         debug.Print("Application terminated");
  213.     }
  214.     catch(...){}
  215.         
  216.     return S_OK;
  217. }
  218.  
  219. HRESULT APPLICATION::Quit()
  220. {
  221.     ::DestroyWindow(m_mainWindow);
  222.     ::PostQuitMessage(0);
  223.     return S_OK;
  224. }